package MovieGuessingGame.login;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.commons.codec.binary.Base64;

class ClientSSL {
	
	SSLSocket sslsocket = null;	
	BufferedReader bufferedreader = null;	
	PrintWriter printwriter = null;
	String message = null;

	byte[] hashValue = null;
	
	public String getMessage() {
		return message;
	}
	
	void clientConnect (String host) {
		try {
			System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\Bob\\workspace\\MovieGuessingGame\\pkeystore.jks");
			SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, 9999);
			
			InputStream inputstream = System.in;
			InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
			BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
			
			printwriter = new PrintWriter(sslsocket.getOutputStream(), true);
			
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
	
	void clientSend(String message) throws IOException{
		printwriter.println(message)
	}
	
	void loginSend(String userName, String password) {
		clientSend(userName + "," + password);
	}
	
	int clientReceive() throws IOException{
		
		String response = bufferedreader.readLine();
		
		return response;
	}
	
	int loginReceive() {
		int retVal = 0;
		
		String result = clientReceive();
		if (retvalue.trim().equals("you are not authorized")
				|| retvalue.equals("try again later"))
		{
			message = retvalue;
			retVal = -1;
		}
		else 
		{
			hashValue = Base64.decodeBase64String(result);
			retVal = 0;
		}
		
		return retVal;
	}
	
	
	public static void main(String[] arstring) {
		ClientSSL client = new ClientSSL();
		try {
			client.clientConnect("localhost");
			String a = "a";

			client.bufferedwriter.write("userName"+ "," + "password" + '\n');
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
}
